#include <iostream>
#include <stack>

using namespace std;

class Solution {
public:
    int calculate(string s) {
        stack<int> num_stack;
        stack<char> operate_stack;
        bool is_num = false;
        int num_begin = 0;
        int num_length = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s[i] >= '0' && s[i] <= '9') {
                if (!is_num) {
                    num_begin = i;
                }
                num_length++;
                is_num = true;
            } else {
                if (is_num) {
                    int num = atoi(s.substr(num_begin, num_length).data());
                    is_num = false;
                    num_length = 0;
                    if (operate_stack.size() > 0 && operate_stack.top() != '(' && num_stack.size() > 0) {
                        char operate = operate_stack.top();
                        int num1 = num_stack.top();
                        operate_stack.pop();
                        num_stack.pop();
                        switch (operate) {
                            case '+':
                                num = num1 + num;
                                break;
                            case '-':
                                num = num1 - num;
                                break;
                            default:
                                break;
                        }
                    }
                    num_stack.push(num);
                }
                if (s[i] == '+') {
                    operate_stack.push('+');
                } else if (s[i] == '-') {
                    operate_stack.push('-');
                } else if (s[i] == '(') {
                    operate_stack.push('(');
                } else if (s[i] == ')') {
                    operate_stack.pop();
                    while (operate_stack.size() > 0 && operate_stack.top() != '(') {
                        if (operate_stack.top() == '+') {
                            int num1 = num_stack.top();
                            num_stack.pop();
                            int num2 = num_stack.top();
                            num_stack.pop();
                            operate_stack.pop();
                            num_stack.push(num1 + num2);
                        } else if (operate_stack.top() == '-') {
                            int num1 = num_stack.top();
                            num_stack.pop();
                            int num2 = num_stack.top();
                            num_stack.pop();
                            operate_stack.pop();
                            num_stack.push(num2 - num1);
                        }
                    }
                }
            }
        }
        if (is_num) {
            num_stack.push(atoi(s.substr(num_begin, num_length).data()));
        }
        while (operate_stack.size() > 0) {
            if (operate_stack.top() == '(') {
                operate_stack.pop();
            } else if (operate_stack.top() == '+') {
                int num1 = num_stack.top();
                num_stack.pop();
                int num2 = num_stack.top();
                num_stack.pop();
                operate_stack.pop();
                num_stack.push(num1 + num2);
            } else if (operate_stack.top() == '-') {
                int num1 = num_stack.top();
                num_stack.pop();
                int num2 = num_stack.top();
                num_stack.pop();
                operate_stack.pop();
                num_stack.push(num2 - num1);
            }
        }
        return num_stack.top();
    }
};

int main() {
    Solution solution;
    int output = solution.calculate("(1+(4+5+2)-3)+(6+8)");
    cout << output << endl;
    return 0;
}